package com.xy.Web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xy.Dao.Impl.NewsDaoImpl;
import com.xy.entity.News;

public class SelServlet extends HttpServlet {

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out=response.getWriter();
		NewsDaoImpl news=new NewsDaoImpl();
		List<News> list=news.getAll();
		News newss=null;
		StringBuffer newsJSON=new StringBuffer("[");
		for (int i =0;;) {
			newss=list.get(i);
			newsJSON.append("{\"nid\"}:"+newss.getNid()+",");
			newsJSON.append("{\"ntitle\"}:"+newss.getNtitle().replace("\"", "&quot;")+"\",");
			newsJSON.append("{\"nauthor\"}:"+newss.getNauthor().replace("\"", "&quot;")+"\",");
			if ((++i)==list.size()) {
				break;
			}else{
				newsJSON.append(",");
			}
		}
		newsJSON.append("]");
		out.print(newsJSON);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

	

}
